package vivek.project.pocketmoney.common.utils
{
	import mx.utils.StringUtil;

	public class DateUtils
	{
		public static const millisecondsPerDay:int=1000 * 60 * 60 * 24;

		public static function getDateFromString(format:String, dateString:String):Date
		{
			var temp:int=format.indexOf("D", 0);
			var temp2:int=format.lastIndexOf("D");
			var dString:String=dateString.substr(temp, temp2 + 1);
			temp=format.indexOf("M", 0);
			temp2=format.lastIndexOf("M");
			var mString:int=new Number(dateString.substr(temp, temp2 - temp + 1)) - 1;
			temp=format.indexOf("Y", 0);
			temp2=format.lastIndexOf("Y");
			var yString:String="2000";
			if ((temp2 - temp + 1) == 2)
				yString="20" + dateString.substr(temp, temp2 - temp + 1);
			else
				yString=dateString.substr(temp, temp2 - temp + 1);
			return new Date(yString, mString, dString);
		}

		public static function dateAdd(datepart:String="", number:Number=0, date:Date=null):Date
		{
			if (date == null)
			{
				/* Default to current date. */
				date=new Date();
			}
			var returnDate:Date=new Date(date.time);
			switch (datepart.toLowerCase())
			{
				case "fullyear":
				case "month":
				case "date":
				case "hours":
				case "minutes":
				case "seconds":
				case "milliseconds":
					returnDate[datepart]+=number;
					break;
				default: /* Unknown date part, do nothing. */
					break;
			}
			return returnDate;
		}

		public static function dateCompare(date1:Date, date2:Date, exactMatch:Boolean=false):int
		{
			if (exactMatch == true)
			{
				if (date1.time == date2.time)
					return 0;
				else if (date1.time < date2.time)
					return 1;
				else if (date1.time > date2.time)
					return -1;
			}
			else
			{
				var _year1:int=date1.fullYear;
				var _month1:int=date1.month;
				var _date1:int=date1.date;
				var _year2:int=date2.fullYear;
				var _month2:int=date2.month;
				var _date2:int=date2.date;
				var tempDate1:Date=new Date(_year1, _month1, _date1);
				var tempDate2:Date=new Date(_year2, _month2, _date2);
				if (tempDate1.time == tempDate2.time)
					return 0;
				else if (tempDate1.time < tempDate2.time)
					return 1;
				else if (tempDate1.time > tempDate2.time)
					return -1;
			}
			return 0;
		}

		public static function dateDiff(date1:Date, date2:Date):Number
		{
			return Math.ceil((date1.getTime() - date2.getTime()) / millisecondsPerDay);
		}

		public function DateUtils()
		{
		}
	}
}